/*
 * Copyright:  2018 smarabbit studio.
 *
 * Licensed under the Confluent Community License; you may not use this file except in
 * compliance with the License.  You may obtain a copy of the License at
 *
 * http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software distributed under
 *  the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 *  either express or implied.  See the License for the specific language governing
 *  permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:    2020/8/16 上午10:35
 *
 */

package com.massyframework.beanskin.graphql.fetching;

import graphql.language.*;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import static graphql.Assert.assertNotNull;

/**
 * {@link CustomizeField},自定义字段
 * @author huangkh
 *
 */
public class CustomizeField extends Field {

    /**
	 * 
	 */
	private static final long serialVersionUID = -2771956983326151226L;
	private MergedFieldFactory<?> factory;

    /**
     * 构造方法
     * @param name
     * @param alias
     * @param arguments
     * @param directives
     * @param selectionSet
     * @param sourceLocation
     * @param comments
     * @param ignoredChars
     * @param additionalData
     * @param factory
     */
    protected CustomizeField(String name,
                          String alias,
                          List<Argument> arguments,
                          List<Directive> directives,
                          SelectionSet selectionSet,
                          SourceLocation sourceLocation,
                          List<Comment> comments,
                          IgnoredChars ignoredChars,
                          Map<String, String> additionalData,
                          MergedFieldFactory<?> factory) {
        super(name, alias, arguments, directives, selectionSet, sourceLocation, comments, ignoredChars, additionalData);
        this.factory = factory;
    }

    /**
     * 创建构建器
     * @return {@link Builder}
     */
    public static Builder newBuilder(){
        return new Builder();
    }

    /**
     * 使用{@code name}创建构建器
     * @param name {@link String}, 字段名称
     * @return {@link Builder}
     */
    public static Builder newBuilder(String name){
        return new Builder().name(name);
    }

    public static Builder newBuilder(Field field){
        return new Builder()
                .name(field.getName())
                .alias(field.getAlias())
                .directives(field.getDirectives())
                .additionalData(field.getAdditionalData())
                .arguments(field.getArguments())
                .selectionSet(field.getSelectionSet())
                .sourceLocation(field.getSourceLocation())
                .ignoredChars(field.getIgnoredChars())
                .comments(field.getComments());
    }

    public MergedFieldFactory<?> getMergedFieldFactory(){
        return this.factory;
    }

    public static final class Builder implements NodeBuilder {
        private SourceLocation sourceLocation;
        private List<Comment> comments = new ArrayList<>();
        private String name;
        private String alias;
        private List<Argument> arguments = new ArrayList<>();
        private List<Directive> directives = new ArrayList<>();
        private SelectionSet selectionSet;
        private IgnoredChars ignoredChars = IgnoredChars.EMPTY;
        private Map<String, String> additionalData = new LinkedHashMap<>();
        private MergedFieldFactory<?> factory;

        private Builder() {
        }

        private Builder(Field existing) {
            this.sourceLocation = existing.getSourceLocation();
            this.comments = existing.getComments();
            this.name = existing.getName();
            this.alias = existing.getAlias();
            this.arguments = existing.getArguments();
            this.directives = existing.getDirectives();
            this.selectionSet = existing.getSelectionSet();
            this.ignoredChars = existing.getIgnoredChars();
            this.additionalData = new LinkedHashMap<>(existing.getAdditionalData());
        }


        public Builder sourceLocation(SourceLocation sourceLocation) {
            this.sourceLocation = sourceLocation;
            return this;
        }

        public Builder comments(List<Comment> comments) {
            this.comments = comments;
            return this;
        }

        public Builder name(String name) {
            this.name = name;
            return this;
        }

        public Builder alias(String alias) {
            this.alias = alias;
            return this;
        }

        public Builder arguments(List<Argument> arguments) {
            this.arguments = arguments;
            return this;
        }

        public Builder directives(List<Directive> directives) {
            this.directives = directives;
            return this;
        }

        public Builder selectionSet(SelectionSet selectionSet) {
            this.selectionSet = selectionSet;
            return this;
        }

        public Builder ignoredChars(IgnoredChars ignoredChars) {
            this.ignoredChars = ignoredChars;
            return this;
        }

        public Builder additionalData(Map<String, String> additionalData) {
            this.additionalData = assertNotNull(additionalData);
            return this;
        }

        public Builder additionalData(String key, String value) {
            this.additionalData.put(key, value);
            return this;
        }

        public Builder factory(MergedFieldFactory<?> factory){
            this.factory = factory;
            return this;
        }

        /**
         * 构建自定义字段
         * @return {@link CustomizeField}
         */
        public CustomizeField build() {
            return new CustomizeField(
                    name,
                    alias,
                    arguments,
                    directives,
                    selectionSet,
                    sourceLocation,
                    comments,
                    ignoredChars,
                    additionalData,
                    factory);
        }
    }
}
